<?php

require_once(APPPATH . 'libraries/MY_PortalController.php');

class Admin extends MY_PortalController {

	var $error = false;
	var $cant_per_page = 5;

	function Admin() {
		parent::MY_PortalController();
		//Controlo que el usuario este logueado
		$user = $this->getUserLogued();
		if ($user == NULL) {
			redirect("/main/pc");
			$this->error = true;
		} elseif ($user->is_admin == 0) {
			show_404('Acceso denegado!');
			$this->error = true;
		}
	}

	function index() {
		if ($this->error)return;
		$data = $this->find_channels();
		$this->load->view('backend/canales', $data);
	}

	function find_backend_channels() {
		if ($this->error)return;
		$search = $this->input->post('busqueda');
		$page = $this->input->post('page');
		$data = $this->find_channels($search, $page);
		$html = $this->load->view('backend/canales_data_table', $data, true);
		$this->returnJson(array("page" => $data["page"], "pages" => $data["pages"], "html" => $html));
	}

	private function find_channels($search="", $page=1) {
		$this->load->model('m_channel');
		$cant = $this->m_channel->get_channels_count($search);
		$pages = max(1,ceil($cant / $this->cant_per_page));
		$page = max(1,$page);

		if($page>$pages)
			$page=$pages;
		
		$result = $this->m_channel->get_all_channels($search, $page, $this->cant_per_page);

		//Se agregan los thumbs
		$this->load->model("m_channel_thumb");
		foreach($result as $c){
			$thumb = $this->m_channel_thumb->getRandThumb($c->suite_id);
			$c->thumb = $thumb->content;
		}

		return array("channels" => $result, "pages" => (int)$pages, "page" => (int)$page);
	}

	/*****************************************************************
	 * DENUNCIAS
	 */

	function get_denuncias() {
		if ($this->error)return;

		$this->load->model('m_denuncia');

		$channel_id = $this->input->post('channel_id');
		$data = $this->m_denuncia->getDenuncias($channel_id);

		$this->load->view('backend/denuncias', $data);
	}

	function borrar_denuncia() {
		if ($this->error)return;

		$this->load->model('m_denuncia');

		$denuncia_id = $this->input->post('denuncia_id');
		$channel_id = $this->input->post('channel_id');

		$this->m_denuncia->deleteById($denuncia_id);
		$data = $this->m_denuncia->getDenuncias($channel_id);

		$this->load->view('backend/denuncias', $data);
	}

	/*****************************************************************
	 * CANALES
	 */

	/* SI SE DESCOMENTA ESTA HABRIA QUE ELIMINARLO DE LA SUITE TMB.
	 * function delete_channel() {
		if($this->error)return;
		$this->load->model('m_channel');

		$channel_id = $this->input->post('channel_id');
		$data = $this->m_channel->deleteById($channel_id);
	}*/

	function changeStatusChannel(){
		if($this->error)return;
		$next_status = $this->input->post('next_status');
		$channel_id = $this->input->post('channel_id');
		try{
			$this->load_api();
			$this->api->update_signal_status( $channel_id, $next_status);

			$this->load->model('m_channel');
			$channel = $this->m_channel->getById($channel_id);
			$channel->active = $next_status;
			$this->m_channel->update($channel);
			$ret= $this->getDataRet(true);
		}catch(Exception $ex){
			$ret = $this->getDataRet(false, "Ocurrio un error al tratar de actualizar el estado: ".$ex->getMessage());
		}
		$this->returnJson($ret);
	}

}

?>
